// pages/home/list.js
const net = require('../../../utils/net.js');
const util = require('../../../utils/util.js');
var posting = false;
var dialog = null;
const gd = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logo: "../../../assets/image/logo.png",
    list:[],
    page: 1, 
    scrollTop: 0,
    scrolltmp: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  getlist() {
    if (posting) return;
    posting = true;
    var that = this;
    wx.showLoading({})
    net.request({
      url: '/PrivateLessonList',
      data: {
        shop_id: gd.shop_id,
        member_id: gd.id,
        page: that.data.page
      }, success(res) {
        var dt = res.data;
        if (dt.flag) {
          if (dt.data.length > 0) {
            var tmplist = that.data.list;
            for (var i = 0; i < dt.data.length; ++i) {
              var time = dt.data[i]["start_date"].replace(/\-/g, ".") + "-" + dt.data[i]["end_date"].replace(/\-/g, ".");
              dt.data[i]["time"] = time;
              tmplist.push(dt.data[i]);
            }
            that.setData({
              page: that.data.page+1,
              list: tmplist
            })
          } else {

          }
        } else {
          dialog.showDialog({
            msg: dt.msg
          })
        }
      }, fail(res) {

      }, complete() {
        posting =false;
        wx.hideLoading();
        wx.stopPullDownRefresh();
        that.scrollloading(false);
      }
    })
  }, goclassdetail(e) {
    var id = e.currentTarget.dataset.id;
    util.Forward("../classdetail/classdetail?id=" + id);
  },
  toscroll(e) {
    console.log("toscroll",e);
    this.setData({
      scrolltmp: e.detail.scrollTop
    })
  }, tolower() {
    console.log("tolower");
    this.scrollloading(true);
    this.getlist();
  }, scrollloading(isshow) {
    var that = this;
    let top = 0;
    let delay = 0;
    if (isshow) {
      top = that.data.list.length * 60;
    } else {
      delay = 100;
      top = that.data.scrolltmp - 60;
    }
    setTimeout(function () {
      that.setData({
        scrollTop: top
      })
    }, delay);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    dialog = this.selectComponent("#dialog");
    this.getlist();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page:1,
      list:[]
    })
    this.getlist();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})